﻿
namespace ShopOnline.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Linq;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Microsoft.ServiceModel.DomainServices.LinqToSql;


    // Implements application logic using the ShopOnlineDataContext context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class DomainServiceShopOnline : LinqToSqlDomainService<ShopOnlineDataContext>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        public IQueryable<Category> GetCategories()
        {
            return this.DataContext.Categories.OrderBy(category => category.CategoryName);
        }

        public void InsertCategory(Category category)
        {
            this.DataContext.Categories.InsertOnSubmit(category);
        }

        public void UpdateCategory(Category currentCategory)
        {
            this.DataContext.Categories.Attach(currentCategory, this.ChangeSet.GetOriginal(currentCategory));
        }

        public void DeleteCategory(Category category)
        {
            this.DataContext.Categories.Attach(category);
            this.DataContext.Categories.DeleteOnSubmit(category);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        [RequiresAuthentication]
        public IQueryable<Order> GetOrders()
        {
            return this.DataContext.Orders.OrderByDescending(order => order.Date);
        }

        public void InsertOrder(Order order)
        {
            order.OrderStatusID = 1;
            order.Date = DateTime.Now;
            this.DataContext.Orders.InsertOnSubmit(order);
        }
                
        public void UpdateOrder(Order currentOrder)
        {
            this.DataContext.Orders.Attach(currentOrder, this.ChangeSet.GetOriginal(currentOrder));
        }

        public void DeleteOrder(Order order)
        {
            this.DataContext.Orders.Attach(order);
            this.DataContext.Orders.DeleteOnSubmit(order);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        public IQueryable<OrderProduct> GetOrderProducts()
        {
            return this.DataContext.OrderProducts;
        }

        public void InsertOrderProduct(OrderProduct orderProduct)
        {
            this.DataContext.OrderProducts.InsertOnSubmit(orderProduct);
        }

        public void UpdateOrderProduct(OrderProduct currentOrderProduct)
        {
            this.DataContext.OrderProducts.Attach(currentOrderProduct, this.ChangeSet.GetOriginal(currentOrderProduct));
        }

        public void DeleteOrderProduct(OrderProduct orderProduct)
        {
            this.DataContext.OrderProducts.Attach(orderProduct);
            this.DataContext.OrderProducts.DeleteOnSubmit(orderProduct);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        public IQueryable<OrderStatuse> GetOrderStatuses()
        {
            return this.DataContext.OrderStatuses;
        }

        public void InsertOrderStatuse(OrderStatuse orderStatuse)
        {
            this.DataContext.OrderStatuses.InsertOnSubmit(orderStatuse);
        }

        public void UpdateOrderStatuse(OrderStatuse currentOrderStatuse)
        {
            this.DataContext.OrderStatuses.Attach(currentOrderStatuse, this.ChangeSet.GetOriginal(currentOrderStatuse));
        }

        public void DeleteOrderStatuse(OrderStatuse orderStatuse)
        {
            this.DataContext.OrderStatuses.Attach(orderStatuse);
            this.DataContext.OrderStatuses.DeleteOnSubmit(orderStatuse);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        public IQueryable<Product> GetProducts()
        {
            return this.DataContext.Products;
        }

        public void InsertProduct(Product product)
        {
            this.DataContext.Products.InsertOnSubmit(product);
        }

        public void UpdateProduct(Product currentProduct)
        {
            this.DataContext.Products.Attach(currentProduct, this.ChangeSet.GetOriginal(currentProduct));
        }

        public void DeleteProduct(Product product)
        {
            this.DataContext.Products.Attach(product);
            this.DataContext.Products.DeleteOnSubmit(product);
        }
    }
}


